package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MultiAdapter extends BaseAdapter {
	LayoutInflater mInflater;
	ArrayList<ListItemEx> arSrc;
	
	
	
	public MultiAdapter(Context context, ArrayList<ListItemEx> arSrc) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.arSrc = arSrc;
	}

	@Override
	public int getCount() {
		return arSrc.size();
	}

	@Override
	public Object getItem(int position) {
		return arSrc.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public int getItemViewType(int potion){
		return arSrc.get(potion).type;
	}
	
	public int getViewTypeCount(){
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView == null){
			int res = 0;
			switch(arSrc.get(position).type){
			case 0:
				res = R.layout.ch12_textedit;
				break;
			case 1:
				res = R.layout.ch12_btnicon;
				break;
			}
			convertView = mInflater.inflate(res, parent, false);
		}
		
		
		switch(arSrc.get(position).type){
		case 0:
			TextView txt = (TextView)convertView.findViewById(R.id.ch12_textedit_tv);
			txt.setText(arSrc.get(position).text);
			EditText edit = (EditText) convertView.findViewById(R.id.ch12_textedit_et);
			edit.setText(arSrc.get(position).editText);
			break;
		case 1:
			Button btn = (Button) convertView.findViewById(R.id.ch12_btnicon_btn);
			btn.setText(arSrc.get(position).btnText);
			ImageView img = (ImageView) convertView.findViewById(R.id.ch12_btnicon_img);
			img.setImageResource(arSrc.get(position).iconRes);
			break;
		}
		
		return convertView;
	}

}
